﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>CheckBoxList</title>
</head>
<body>
    <div>
        @Html.CheckBoxList("test", "2,3", new List<SelectListItem> {
            new SelectListItem{Text="1",Value="1"},
            new SelectListItem{Text="2",Value="2"},
            new SelectListItem{Text="3",Value="3"},
            new SelectListItem{Text="4",Value="4"},
            new SelectListItem{Text="5",Value="5"}
        })
    </div>
    <script src="~/Content/js/jquery-1.8.2.js"></script>
    <script type="text/javascript">
        //设置多选项框的值
        function SetCheckValue(source) {
            var values = [];
            var $parent = $(source).parent();
            $parent.find("input[type=checkbox]").each(function () {
                if (this.checked) {
                    values.push($(this).val());
                    $(this).attr("checked", "checked");
                } else {
                    $(this).removeAttr("checked");
                }
            });
            $parent.find("input[type=hidden]").val(values.join(","));
        }
    </script>
</body>
</html>
